package com.yanqu.road.dao.impl.hunt;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.hunt.UserHunt;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserHuntDaoImpl extends TempDataObjectDao<UserHunt> {

    public UserHunt getUserHunt(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_hunt where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserHunt data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean addData(UserHunt data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_hunt(userId,monsterIndex,monsterLoseBlood,modifyTime,openBoxTimes,openHuntBoxUidList,huntScore,maxLevel) values (?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getMonsterIndex());
            call.setString(i++, data.getMonsterLoseBlood().toString());
            call.setLong(i++, data.getModifyTime());
            call.setInt(i++, data.getOpenBoxTimes());
            call.setString(i++, data.getOpenHuntBoxUidListString());
            call.setString(i++, data.getHuntScore().toString());
            call.setInt(i++,data.getMaxLevel());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserHunt data) {
        PreparedStatement call = null;
        String sql = "update t_u_hunt set monsterIndex=?,monsterLoseBlood=?,modifyTime=?,openBoxTimes=?,openHuntBoxUidList=?,huntScore=?,maxLevel=? where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getMonsterIndex());
            call.setString(i++, data.getMonsterLoseBlood().toString());
            call.setLong(i++, data.getModifyTime());
            call.setInt(i++, data.getOpenBoxTimes());
            call.setString(i++, data.getOpenHuntBoxUidListString());
            call.setString(i++, data.getHuntScore().toString());
            call.setInt(i++,data.getMaxLevel());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserHunt parseData(ResultSet rs) throws SQLException {
        UserHunt data = new UserHunt();
        data.setUserId(rs.getLong("userId"));
        data.setMonsterIndex(rs.getInt("monsterIndex"));
        String value = rs.getString("monsterLoseBlood");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setMonsterLoseBlood(new BigInteger(value));
        }
        data.setModifyTime(rs.getLong("modifyTime"));
        data.setOpenBoxTimes(rs.getInt("openBoxTimes"));
        data.setOpenHuntBoxUidList(StringUtils.stringToStringList(rs.getString("openHuntBoxUidList"), ","));
        value = rs.getString("huntScore");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setHuntScore(new BigInteger(value));
        }
        data.setMaxLevel(rs.getInt("maxLevel"));
        return data;
    }
}
